<?php

namespace app\adminapi\controller\system;

use app\adminapi\service\SysConfigService;
use app\adminapi\validate\SysConfigValidate;
use app\common\controller\BaseAdminController;
use app\common\exception\OtherException;

class SysConfigController extends BaseAdminController
{
    public array $notNeedLogin = [];

    public function list()
    {
        $param = (new SysConfigValidate())->get()->goCheck('list');
        $result = (new SysConfigService())->list($param);
        return app('json')->successWithData($result);
    }

    public function listByConfigKey()
    {
        $param = (new SysConfigValidate())->get()->goCheck('listByConfigKey');
        if (isset($param['configKey']) && !is_array($param['configKey'])) {
            throw new OtherException('configKey必须为数组');
        }
        $result = (new SysConfigService())->listByConfigKey($param['configKey']);
        return app('json')->successWithData($result);
    }

    /**
     * @title 新增配置
     * @businessType INSERT
     */
    public function insert()
    {
        $param = (new SysConfigValidate())->post()->goCheck('insert');
        $result = (new SysConfigService())->addSysConfig($param);
        return app('json')->successWithData($result);
    }

    /**
     * @title 删除配置
     * @businessType DELETE
     */
    public function delete()
    {
        $param = (new SysConfigValidate())->delete()->goCheck('delete');
        $result = (new SysConfigService())->deleteSysConfig($param['sysConfigId']);
        return app('json')->successWithData($result);
    }

    /**
     * @title 修改配置
     * @businessType UPDATE
     */
    public function update()
    {
        $param = (new SysConfigValidate())->put()->goCheck('update');
        $result = (new SysConfigService())->updateSysConfig($param);
        return app('json')->successWithData($result);
    }

    public function query()
    {
        $param = (new SysConfigValidate())->get()->goCheck('query');
        $result = (new SysConfigService())->getSysConfig($param['sysConfigId']);
        return app('json')->successWithData($result);
    }
}